package de.tum.beilmaier.im.client;

import java.util.Map.Entry;

import javax.servlet.http.HttpSession;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.TabBar.Tab;
import com.google.gwt.user.client.ui.TabPanel;

import de.novanic.eventservice.client.event.Event;
import de.novanic.eventservice.client.event.RemoteEventService;
import de.novanic.eventservice.client.event.RemoteEventServiceFactory;
import de.novanic.eventservice.client.event.domain.DomainFactory;
import de.tum.beilmaier.im.client.bundle.ChatRoomBundle;
import de.tum.beilmaier.im.client.controller.ChatRoomController;
import de.tum.beilmaier.im.client.controller.ContactListController;
import de.tum.beilmaier.im.client.controller.LoginWindowController;
import de.tum.beilmaier.im.client.controller.LogoutWindowController;
import de.tum.beilmaier.im.client.event.LoginEvent;
import de.tum.beilmaier.im.client.view.ChatRoomWindow;
import de.tum.beilmaier.im.client.view.ContactListWindow;
import de.tum.beilmaier.im.client.view.LoginWindow;
import de.tum.beilmaier.im.client.view.LogoutWindow;
import de.tum.beilmaier.im.shared.model.ChatRoom;
import de.tum.beilmaier.im.shared.model.UUID;
import de.tum.beilmaier.im.shared.model.User;



public class InstantChat implements EntryPoint {

	private UserSessionHandler userhandler = null;
	TabPanel chatRoomPanel = null;

	
	public void onModuleLoad() {
		System.out.println(this);

		userhandler = new UserSessionHandler();
		switchToLoginView();

	}

	public void switchToLoginView() {

		RootPanel.get().clear();
		LoginWindow loginWindow = new LoginWindow();
		LoginWindowController loginController = new LoginWindowController(loginWindow, userhandler, this);
		RootPanel.get().add(loginWindow);
	}

	public void switchToMainView() {

		RootPanel.get().clear();
		ContactListWindow contactListWindow = new ContactListWindow();
		ContactListController contactListController = new ContactListController(contactListWindow, userhandler, this);

		LogoutWindow LogoutWindow = new LogoutWindow();
		LogoutWindowController logoutWindowController = new LogoutWindowController(LogoutWindow, userhandler, this);

		chatRoomPanel = new DecoratedTabPanel();
		chatRoomPanel.setSize("800px", "600px");
		RootPanel.get().setSize("800px", "600px");
		chatRoomPanel.setAnimationEnabled(true);
		RootPanel.get().add(contactListWindow);
		RootPanel.get().add(chatRoomPanel);
		chatRoomPanel.setVisible(false);
		RootPanel.get().add(LogoutWindow);
	}

	public void switchViewToChatWindow(ChatRoomBundle chatRoomBundle) {
		System.out.println(chatRoomBundle.getChatRoom().getChatPartners().size());
		String titleUser = "";
		for (Entry<String, User> user : chatRoomBundle.getChatRoom().getChatPartners())
			titleUser = titleUser + " / " + user.getValue().getName();

		if (chatRoomBundle.getTabPanelIndex() == -1) {
			chatRoomPanel.add(chatRoomBundle.getChatRoomWindow(), titleUser, false);
			int index = chatRoomPanel.getTabBar().getTabCount();
			System.out.println(index);
			chatRoomPanel.selectTab(index - 1);
			chatRoomBundle.setTabPanelIndex(index - 1);
		}

	}

	public void createNewChatWindow(ChatRoomBundle chatRoomBundle) {

		System.out.println(chatRoomBundle.getChatRoom().getChatPartners().size());
		String titleUser = "";
		for (Entry<String, User> user : chatRoomBundle.getChatRoom().getChatPartners())
			titleUser = titleUser + " / " + user.getValue().getName();

		chatRoomPanel.add(chatRoomBundle.getChatRoomWindow(), titleUser, false);
		int index = chatRoomPanel.getTabBar().getTabCount();
		System.out.println(index);
		chatRoomPanel.selectTab(index - 1);
		chatRoomBundle.setTabPanelIndex(index - 1);
		chatRoomBundle.getChatRoomWindow().setSize("800px", "600px");
		chatRoomPanel.setVisible(true);

		System.out.println("generate ChatWindow Tab..!");
	}

	public void switchToLogoutView() {
		Window.Location.reload();
		RootPanel.get().clear();
		LoginWindow loginWindow = new LoginWindow();
		LoginWindowController loginController = new LoginWindowController(loginWindow, userhandler, this);
		RootPanel.get().add(loginWindow);

	}

	public TabPanel getTabPanel() {
		return chatRoomPanel;
	}

}
